from flask import Flask, render_template, request, escape, redirect, url_for
from weather import weatherinfo
from forecast import row_data, row_data_shanghai, row_data_anhui, row_data_guangdong
from poi import searcharound
from log_sys import log_request

key = "21c4d9d81092ea5963a76a2e02269961"

app = Flask(__name__)


# 登陆界面
@app.route('/search4', methods=['POST'])
def do_search() -> 'html':
    return render_template('results.html')



@app.route('/')
@app.route('/login')
def entry_page() -> 'html':
    return render_template('login.html')


# home界面跳转到查询天气界面
@app.route('/cool_form', methods=['GET', 'POST'])
def cool_form() -> 'html':
    if request.method == 'POST':
        # do stuff when the form is submitted
        # redirect to end the POST handling
        # the redirect can be to the same route or somewhere else
        return redirect(url_for('index'))
    # show the form, it wasn't submitted
    return render_template('entry.html')

# 当前天气查询跳转界面
@app.route('/check', methods=['POST'])
def weather_search() -> 'html':
    city = request.form['city']
    results_weather = str(weatherinfo(key, city))
    results_temperature = str(weatherinfo(key, city))
    results_winddirection = str(weatherinfo(key, city))
    log_request(request, results_weather)
    return render_template('weather_results.html',
                           the_city=city,
                           the_results_weather=results_weather,
                           the_results_temperature=results_temperature,
                           the_results_winddirection=results_winddirection)



# home界面跳转到天气预报
@app.route('/forecast_home', methods=['GET', 'POST'])
def forecast_home() -> 'html':
    if request.method == 'POST':
        return redirect(url_for('index'))
    return render_template('forecast_home.html')



# 北京
@app.route('/beijing', methods=['GET', 'POST'])
def forecast() -> 'html':
    if request.method == 'POST':
        return redirect(url_for('index'))
    data = row_data()
    title_data = data[0]
    td_data = data[2]
    return render_template('forecast_beijing.html',
                           the_title_data=title_data,
                           the_td_data=td_data)


# 上海
@app.route('/shanghai', methods=['GET', 'POST'])
def shanghai_forecast() -> 'html':
    if request.method == 'POST':
        return redirect(url_for('index'))
    data = row_data_shanghai()
    title_data_shanghai = data[0]
    td_data_shanghai = data[2]
    return render_template('forecast_shanghai.html',
                           the_title_data_shanghai=title_data_shanghai,
                           the_td_data_shanghai=td_data_shanghai)


# 安徽
@app.route('/anhui', methods=['GET', 'POST'])
def anhui_forecast() -> 'html':
    if request.method == 'POST':
        return redirect(url_for('index'))
    data = row_data_anhui()
    title_data_anhui = data[0]
    td_data_anhui = data[2]
    return render_template('forecast_anhui.html',
                           the_title_data_anhui=title_data_anhui,
                           the_td_data_anhui=td_data_anhui)


# 广东
@app.route('/guangdong', methods=['GET', 'POST'])
def guangdong_forecast() -> 'html':
    if request.method == 'POST':
        return redirect(url_for('index'))
    data = row_data_guangdong()
    title_data_guangdong = data[0]
    td_data_guangdong = data[2]
    return render_template('forecast_guangdong.html',
                           the_title_data_guangdong=title_data_guangdong,
                           the_td_data_guangdong=td_data_guangdong)


# home界面跳转到查询关键词搜索界面
@app.route('/poi_search', methods=['GET', 'POST'])
def poi_search() -> 'html':
    if request.method == 'POST':
        # do stuff when the form is submitted
        # redirect to end the POST handling
        # the redirect can be to the same route or somewhere else
        return redirect(url_for('index'))
    # show the form, it wasn't submitted
    return render_template('poi.html')


# poi关键词搜索
@app.route('/poi_check', methods=['POST'])
def poi_results() -> 'html':
    keywords = request.form['keywords']
    results = str(searcharound(key, keywords))
    log_request(request, results)
    return render_template('poi_results.html',
                           the_keywords=keywords,
                           the_results=results, )

#用户日志系统
@app.route('/viewlog')
def view_the_log() -> 'html':
    """display the contents of thr log file as a html table"""
    contents = []
    with open('weather.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('用户提交','远程地址','访问代理','用户结果')
    return render_template('viewlog.html',
                           the_title='日志系统后台',
                           the_row_title=titles,
                           the_data=contents)


if __name__ == '__main__':
    app.run(debug=True)
